/**
 * author      : dormi330
 * date        : 2018/6/24
 * project     : mybatis
 * description : TODO
 */

package com.deep.trade.application.order;

import com.deep.trade.adaptor.required.IdService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.deep.trade.application.OrderServiceFacade;
import com.deep.trade.order.InDtoAuditOrder;
import com.deep.trade.order.InDtoCreateOrder;
import com.deep.trade.order.InDtoPayOrderSummary;
import com.deep.trade.order.InDtoPayTradeOrder;
import com.deep.trade.order.query.OutDtoOrderDetail;
import com.deep.trade.order.query.OutDtoOrderSummary;
import com.deep.trade.order.query.OutDtoTradeOrder;

import java.util.List;

public class OrderBaseOperation {

    protected Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    protected final int pageNum = 1;
    protected final int pageSize = 10;
    protected boolean enableDebug = false;
    protected final int EXPECT_TRADE_ORDER_NUM = 2;
    protected final int EXPECT_SKU_NUM_PER_ORDER = 2;

    @Autowired
    protected OrderServiceFacade orderServiceFacade;

    protected void placeOrder() throws Exception {
        /*
         *
         * 同一个用户 在店铺1买了2各sku, 在店铺2买了2各sku, 下单
         * 会产生1个 OrderSummary, 2个 TradeOrder, 每个TradeOrder 会有2个OrderSku
         */
        if (enableDebug) logger.info("【start】placeOrder");

        InDtoCreateOrder inDtoCreateOrder = new InDtoCreateOrder(IdService.TENANT_CODE, IdService.MEMBER_ID, IdService.ADDRESS_ID, "remark1");
        inDtoCreateOrder.addSkuAndQty(IdService.SKU_1_ID, 1);
        inDtoCreateOrder.addSkuAndQty(IdService.SKU_2_ID, 2);
        inDtoCreateOrder.addSkuAndQty(IdService.SKU_3_ID, 3);
        inDtoCreateOrder.addSkuAndQty(IdService.SKU_4_ID, 4);
        logger.info("下单={}", inDtoCreateOrder.toString());

        orderServiceFacade.placeOrder(inDtoCreateOrder);
        if (enableDebug) logger.info("【√】placeOrder - done");
    }

    protected List<OutDtoTradeOrder> tradeOrderListOfMember() throws Exception {
        // ==== check TradeOrder list
        if (enableDebug) logger.info("【start】tradeOrderListOfMember");
        PageInfo<OutDtoTradeOrder> pageInfo = orderServiceFacade.tradeOrderListOfMember(IdService.TENANT_CODE, IdService.MEMBER_ID, pageNum, pageSize);
        List<OutDtoTradeOrder> outDtoTradeOrderList = pageInfo.getList();
        if (enableDebug) logger.info(outDtoTradeOrderList.toString());
        if (enableDebug) logger.info("【√】tradeOrderListOfMember - done");
        return outDtoTradeOrderList;
    }

    protected OutDtoOrderDetail tradeOrderDetail(OutDtoTradeOrder outDtoTradeOrder) throws Exception {

        // ==== check order detail
        if (enableDebug) logger.info("【start】tradeOrderDetail");
        OutDtoOrderDetail outDtoOrderDetail = orderServiceFacade.tradeOrderDetail(IdService.TENANT_CODE, outDtoTradeOrder.getId());
        if (enableDebug) logger.info(outDtoOrderDetail.toString());
        if (enableDebug) logger.info("【√】tradeOrderDetail - done");
        return outDtoOrderDetail;
    }

    protected void auditTradeOrderPass(OutDtoTradeOrder outDtoTradeOrder) throws Exception {
        InDtoAuditOrder inDtoAuditOrder = new InDtoAuditOrder(
                outDtoTradeOrder.getTenantCode(),
                outDtoTradeOrder.getSellerId(),
                outDtoTradeOrder.getId(),
                true,
                null
        );
        orderServiceFacade.auditTradeOrder(inDtoAuditOrder);
    }

    protected void auditTradeOrderAllPass(List<OutDtoTradeOrder> outDtoTradeOrderList) throws Exception {
        for (OutDtoTradeOrder outDtoTradeOrder : outDtoTradeOrderList) {
            InDtoAuditOrder inDtoAuditOrder = new InDtoAuditOrder(
                    outDtoTradeOrder.getTenantCode(),
                    outDtoTradeOrder.getSellerId(),
                    outDtoTradeOrder.getId(),
                    true,
                    null
            );
            orderServiceFacade.auditTradeOrder(inDtoAuditOrder);
        }
    }

    protected void auditTradeOrderReject(OutDtoTradeOrder outDtoTradeOrder) throws Exception {
        InDtoAuditOrder inDtoAuditOrder = new InDtoAuditOrder(
                outDtoTradeOrder.getTenantCode(),
                outDtoTradeOrder.getSellerId(),
                outDtoTradeOrder.getId(),
                false,
                "reject_for_test"
        );

        orderServiceFacade.auditTradeOrder(inDtoAuditOrder);
    }

    protected List<OutDtoOrderSummary> getOrderSummary() throws Exception {
        PageInfo<OutDtoOrderSummary> page = orderServiceFacade.orderSummaryOfMember(IdService.TENANT_CODE, IdService.MEMBER_ID, pageNum, pageSize);
        return page.getList();
    }

    protected void payOrderSummary(OutDtoOrderSummary outDtoOrderSummary) throws Exception {
        InDtoPayOrderSummary inDtoPayOrderSummary = new InDtoPayOrderSummary(
                "pay_type_001",
                "pay_channel_001",
                outDtoOrderSummary.getTenantCode(),
                outDtoOrderSummary.getId()
        );
        orderServiceFacade.payOrderSummary(inDtoPayOrderSummary);
    }

    public void payTradeOrder(OutDtoTradeOrder outDtoTradeOrder) throws Exception {
        InDtoPayTradeOrder inDtoPayTradeOrder = new InDtoPayTradeOrder(
                "pay_type_001",
                "pay_channel_001",
                outDtoTradeOrder.getTenantCode(),
                outDtoTradeOrder.getId()
        );
        orderServiceFacade.payTradeOrder(inDtoPayTradeOrder);
    }

}
